package com.ahpu.jym.messagealert;

import java.io.IOException;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import com.ahpu.jym.messagealert.tools.OptionsData;
import com.ahpu.jym.messagealert.ui.MessageAlertDailog;

import net.rim.blackberry.api.phone.Phone;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Backlight;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;

public class SmsListening extends Thread {

	// private MessageConnection _conn;
	// DatagramConnection _dc;
	/** 短信的连接 **/
	Connection _conn;
	/** 短信发送者 **/
	String address;
	/** 短信内容 **/
	String message;

	/** 保存的数据 **/
	OptionsData data;

	public SmsListening() {
		try {
			//监听短信
			_conn = Connector.open("sms://:0");
			// _dc = (DatagramConnection) Connector.open("sms://");

		} catch (IOException e) {
			errorAlert("1:" + e.getMessage());
		}

	}

	public void run() {
		try {

			//奶奶的，这不就是个死循环吗？
			for (;;) {

				MessageConnection msgConn = (MessageConnection) _conn;
				Message m = msgConn.receive();
				// Datagram d = _dc.newDatagram(_dc.getMaximumLength());
				// _dc.receive(d);
				// byte[] bytes = d.getData();
				// address = d.getAddress();//sms://number
				// DatagramBase base = new DatagramBase();
				// base.setAddress(d);
				// message = new String(bytes,"UTF-16BE");
				//

				// byte[] bytes = d.getData();
				address = m.getAddress();//
				//只监听语言短信
				if (m instanceof TextMessage) {
					TextMessage tm = (TextMessage) m;
					message = tm.getPayloadText();
				}

				data = OptionsData.load();

				if (IsCanAlert(data) && address != null && message != null) {

					if (true) {
						// TODO 139
						Application.getApplication().invokeLater(
								new Runnable() {

									public void run() {
										synchronized (Application
												.getEventLock()) {
											UiEngine ui = Ui.getUiEngine();

											ui.pushGlobalScreen(
													new MessageAlertDailog(
															false, address,
															message, ""), 10,
													UiApplication.GLOBAL_QUEUE);
											if ((Display.getProperties() & Display.DISPLAY_PROPERTY_REQUIRES_BACKLIGHT) != 0
													&& data.getBacklight() == 0) {
												Backlight.enable(true, 30);
											}
										}
										message=null;
										address=null;
									}
								});
					}

				}

			}
		} catch (IOException ioe) {
			errorAlert("2:" + ioe.getMessage());
		} catch (NullPointerException e) {
		}
	}

	private void errorAlert(String msg) {
		synchronized (Application.getEventLock()) {
			UiEngine ui = Ui.getUiEngine();
			Screen screen = new Dialog(Dialog.D_OK, msg, Dialog.OK,
					Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
					Manager.VERTICAL_SCROLL);
			ui.pushGlobalScreen(screen, 10, UiEngine.GLOBAL_QUEUE);
		}
	}

	/**
	 * 
	 *<h4>Title: IsCanAlert</h4>
	 *<p>Description: 是否可以弹出</p>
	 * @param data 数据
	 * @return yes or no
	 */
	private boolean IsCanAlert(OptionsData data) {

		if (data.getStatus() == 0) {
			if (DeviceInfo.isInHolster() && data.getHander() == 1) {
				return false;
			} else {
				if (Phone.getActiveCall() != null && data.getActiveCall() == 1) {
					return false;
				}
				return true;
			}
		}
		return false;
	}

}
